package com.duoduo.devtools.util;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.duoduo.devtools.vo.JavaBeanAttribute;
import com.duoduo.devtools.vo.JavaBeanGenerate;

import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;



public class MetadataUtil {

	/**
	 * 获取参数
	 * @param clzz
	 * @return
	 */
	public static JavaBeanGenerate getParam(Class<?> clzz) {
		String packageName = clzz.getPackageName();
		String serviceName = packageName.split("\\.")[2];//服务名称 类名的第三个
		String path = packageName.substring(0,packageName.length()-7).replace(".", File.separator);
	
		String entityType = clzz.getSimpleName().replace("DTO", "");//类名
		String entity = StrUtil.lowerFirst(entityType);//类名变量(首字母小写)
		String permission = MetadataUtil.upperCharToUnderLine(entityType);
		Annotation[] clAnnotations = clzz.getAnnotations();
		JavaBeanGenerate entityVo = new JavaBeanGenerate();
		entityVo.setEntityModel(entity);
		 for (Annotation annotation : clAnnotations) {
			if(annotation instanceof Tag) {
				Tag apiModel = (Tag) clzz.getAnnotation(Tag.class);
				String entityModel = apiModel.name();
				entityVo.setEntityModel(entityModel);
			}
		}
		//field.explain 说明  field.fieldType //类型  fieldName 属性名
		//entityName 类名
		Field[] fields = clzz.getDeclaredFields();
		entityVo.setEntity(entity);
		entityVo.setPath(path);
		entityVo.setPermission(permission);
		entityVo.setServiceName(serviceName);
		entityVo.setEntityType(entityType);
		
		int i = 0;
		Boolean fieldOne = false;
		List<JavaBeanAttribute> columnVos = new ArrayList<JavaBeanAttribute>();
		 for (Field field : fields) {
			 JavaBeanAttribute columnVo = new JavaBeanAttribute();
			 Annotation[] annotations = field.getAnnotations();
			 columnVo.setExplain("");
			 for (Annotation annotation : annotations) {
				if(annotation instanceof Schema) {
					Schema apiModelProperty = field.getAnnotation(Schema.class);
					 columnVo.setExplain(apiModelProperty.description());
				}
			}
			 
			if(field.getAnnotatedType().toString().equals("java.lang.Long")&&i==0) {
				fieldOne = true;
			}
			 String[] type = field.getAnnotatedType().toString().split("\\.");
			 columnVo.setFieldType(type[type.length-1]);
			 columnVo.setFieldName(field.getName());
			 i++;
			 columnVos.add(columnVo);
		}
		entityVo.setFieldOne(fieldOne);
		entityVo.setFields(columnVos);
		return entityVo;
	}
	
	public static void main(String[] args) {
		String path = "SysDicty";
		System.out.println(MetadataUtil.upperCharToUnderLine(path));
		
	}
	 public static String upperCharToUnderLine(String param) {
	        Pattern p = Pattern.compile("[A-Z]");
	        if (param == null || param.equals("")) {
	            return "";
	        }
	        StringBuilder builder = new StringBuilder(param);
	        Matcher mc = p.matcher(param);
	        int i = 0;
	        while (mc.find()) {
//	            System.out.println(builder.toString());
//	            System.out.println("mc.start():" + mc.start() + ", i: " + i);
//	            System.out.println("mc.end():" + mc.start() + ", i: " + i);
	            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
	            i++;
	        }
	 
	        if ('_' == builder.charAt(0)) {
	            builder.deleteCharAt(0);
	        }
	        return builder.toString();
	    }
}
